﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;
using System.Collections.Generic;

namespace Jy123.Web.Friend
{
    public partial class FriendCongnition : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            int userID;
            string strUserIdFromCookie = Common.Cookie.getCookie("UserID");
            if (string.IsNullOrEmpty(strUserIdFromCookie))
            {
                Response.Redirect("/");
            }
            userID = int.Parse(strUserIdFromCookie);

            //页面初始化
            StringBuilder sbHtml = new StringBuilder(Cache["user_friendvisited"].ToString());//最终写出页面的大sb
            sbHtml = sbHtml.Replace("$Header$", Cache["user_header"].ToString());//头部
            sbHtml = sbHtml.Replace("$Footer$", Cache["user_footer"].ToString());//底部
            sbHtml = sbHtml.Replace("$LeftUserCmd$", Cache["user_navigation_up"].ToString());//左侧导航
            sbHtml = sbHtml.Replace("$LeftUserFriendAdd$", Cache["user_friendadd"].ToString());//左侧直接添加好友
            //------------------------------------------------------------------------
            sbHtml = sbHtml.Replace("$ClassOnline$", "")
                .Replace("$ClassCongnition$", "class=sel")
                .Replace("$ClassIn$", "")
                .Replace("$ClassRandom$", "")
                .Replace("$ClassOut$", "");
            sbHtml = sbHtml.Replace("$Visited_ul$", VisitedUl(userID));
            Response.Write(sbHtml.ToString());
        }
        private string VisitedUl(int userID)
        {
            List<int?> count;
            StringBuilder sbUl = new StringBuilder();
            string strUl = Cache["user_friendvisited_ul"].ToString();
            DataTable dtVisitedList = new Jy123.BLL.User.Friend().FriendCongnition(1, 8, userID, out count).Tables[0];
            for (int i = 0; i < dtVisitedList.Rows.Count; i++)
            {
                //根据用户ID判断用户是否有头像,如果没有头像就返回默认头像地址
                string userPortrait = new Jy123.BLL.User.SPace().DefaultPortrait(Convert.ToInt32(dtVisitedList.Rows[i]["ID"]));

                string str = strUl;
                str = str.Replace("$UserID$", dtVisitedList.Rows[i]["ID"].ToString());
                str = str.Replace("$UserName$", dtVisitedList.Rows[i]["UserName"].ToString());
                str = str.Replace("$Portrait$", userPortrait);
                str = str.Replace("$MoodContent$", Common.Input.GetSubString(dtVisitedList.Rows[i]["mood"].ToString(), 12));
                int isRequested = new BLL.User.Friend().AlreadySearch(userID, Convert.ToInt32(dtVisitedList.Rows[i]["ID"]));
                switch (isRequested)
                {
                    case 2:
                        str = str.Replace("$VisitedTime$",
                        string.Format("<span><a href=\"javascript:;\" onclick =\"myAjax('/home/FriendAddByUserID.ashx','UserID={0}');$(this).parent().html('<span class=red>等待确认</span>');\">加为好友</a></span>", Convert.ToInt32(dtVisitedList.Rows[i]["ID"])));
                        break;
                    case 1:
                        str = str.Replace("$VisitedTime$", "");
                        break;
                    case 0:
                        str = str.Replace("$VisitedTime$", "<span class='red'>等待确认</span>");
                        break;
                }
                str = str.Replace("$VisitedTime$", "");
                sbUl.Append(str);
            }
            string pageHtml = @"
                            <li class=""page"">
                                <span>
                                       {0}
                                </span>
                                <b>第{1}-{2}个/共{3}个</b> 
                            </li>";
            if (count[1] > 1)
            {
                Jy123.Web.AppCode.MoonPage mp = new Jy123.Web.AppCode.MoonPage(true, false);
                mp.Count = Convert.ToInt32(count[1]);
                sbUl.AppendFormat(pageHtml, mp.GetStrPage(1, "no", "sel", "fillhtml('items','/Friend/FriendItems.ashx?page=$num$')"), 1, 8, count[0]);
            }
            //sbUl.Append(@"<script language=""javascript"" type=""text/javascript"">$(function(){bindGiftItems();})</script>");
            return sbUl.ToString();
        }
    }
}
